{extend name="../template/admin/head.html"}
{block name="title"}
微语管理_{__block__}
{/block}
{block name="csssrc"}
<link rel="stylesheet" href="__adminstatic__/layui/css/twitter.css">
{/block}
{block name="content"}
{block name="content"}
    <blockquote class="layui-elem-quote title" style="margin-left:10px;margin-top:10px;"><h2>微语</h2></blockquote>
  <form class="layui-form" >
    
    <div class="layui-form-item">
      <label class="layui-form-label">微语内容:</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入微语内容" class="layui-textarea" id="twitter" style="width: 50%;"></textarea>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit="submit" lay-filter="addNews">立即发布</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
  </form>
  <div class="layui-row" style="margin-left: 10px;">
  {volist name="twitter" id="twitter"}
    <div class="twitter" id="data{$twitter.id}">
      <div class="twitter-img"><img src="__indexstatic__/images/face.png"></div>
      <div class="twitter-info">
      <div class="twitter-name">{$nickname}</div>
      <div class="twitter-date">{$twitter.time|date='Y-m-d',###}</div>
      <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="twdel({$twitter.id})">删除</button>
      </div>
      <div class="twitter-con">{$twitter.content}</div>
    </div>
  {/volist}
  </div>
{/block}
{block name="jssrc"}
<script type="text/javascript">
layui.use('form', function(){
  var form = layui.form;    
 form.on('submit(addNews)', function(date) {
  var twitter = $("#twitter").val();
  if (twitter != "") {
        $.ajax({
          type:"POST",
          url:"{:URL('Twitters/twadd')}",
          dataType:"json",
          data:{content:twitter},
          beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#addNews").attr({"disabled":"disabled"});
            $("#addNews").addClass("layui-btn-disabled");
            $("#addNews").html("发布中...");
            layer.load(1);
            },
           error: function (XmlHttpRequest, textStatus, errorThrown) {
              if (XmlHttpRequest.responseText != "") {
                  layer.msg('提交失败，请稍后尝试！');
                  layer.closeAll('loading');
                  $("#addNews").removeClass("layui-btn-disabled");
                  $("#addNews").removeAttr("disabled");  
                  $("#addNews").html("立即发布");
              }
          },
          success:function(data){
            //1为修改成功
            //2为修改失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#addNews").removeClass("layui-btn-disabled");
              $("#addNews").removeAttr("disabled");  
              $("#addNews").html("立即发布");
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#addNews").removeClass("layui-btn-disabled");
              $("#addNews").removeAttr("disabled");  
              $("#addNews").html("立即发布");
            }
          }
        });
        return false;
    }else{
      layer.msg('所有内容不能为空!');
      return false;
    }
  });
});
//删除导航数据
function twdel(id){
layer.confirm('确认删除？', {
  btn: ['确认','取消'] //按钮
}, function(){
  if (id != "") {
        $.ajax({
          type:"POST",
          url:"{:URL('Twitters/twdel')}",
          dataType:"json",
          data:{id:id},
          beforeSend: function () {
            // 动画显示
            layer.load(1);
            },
           error: function (XmlHttpRequest, textStatus, errorThrown) {
              if (XmlHttpRequest.responseText != "") {
                  layer.msg('提交失败，请稍后尝试！');
              }
          },
          success:function(data){
            //1为删除成功
            //2为删除失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $('#data'+id).remove();
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
            }
          }
        });
        return false;
    }else{
      layer.msg('ID值为空!');
      return false;
    }
});
}
</script>
{/block}